//
//  String+SJRegular.swift
//  Employee
//
//  Created by swordjoy on 2017/3/22.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation

extension SJStringProxy {
  
    var isMeterRead: Bool {
        
        let pattern = "^\\d+\\.?\\d{0,1}$"
        
        return self.regular(with: pattern)
    }
    
    
  // 是否是字符
  var isLetter: Bool {
    let pattern = "^[A-Za-z]+$"
    return regular(with: pattern)
  }
  
  // 是否是数字,包含小数点
  var isNumberAndPoint: Bool {
    let pattern = "^[+]?\\d+(\\.\\d+)?$"
    return self.regular(with: pattern)
  }
  
  func isNumber() -> Bool {
    let pattern = "^[+]?\\d+$"
    return self.regular(with: pattern)
  }
  
  func isURL() -> Bool {
    let pattern = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]"
    return self.regular(with: pattern)
  }
  
  fileprivate func regular(with pattern: String) -> Bool {
    let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
    let res = regex.matches(in: base, options: .reportCompletion, range: NSMakeRange(0, base.characters.count))
    if res.count > 0 {
      return true
    }
    return false
  }
}
